package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import com.atguigu.gmall.model.item.to.SkuDetailTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author:lyd
 **/
@Controller
public class ItemController {

    @Autowired
    SkuDetailFeignClient skuDetailFeignClient;

    /**
     * 跳转到商品详情页面并封装数据
     *
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/{skuId}.html")
    public String index(@PathVariable String skuId, Model model) {
        Long parseSkuId = Long.valueOf(skuId);
        Result<SkuDetailTO> skuDetail = skuDetailFeignClient.getSkuDetail(parseSkuId);

        if (skuDetail.getCode() != 200) {
            return "item/404";
        }
        SkuDetailTO data = skuDetail.getData();
        if (data != null) {
            model.addAttribute("categoryView", data.getCategoryView());
            model.addAttribute("skuInfo", data.getSkuInfo());
            model.addAttribute("price", data.getPrice());
            model.addAttribute("spuSaleAttrList", data.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson", data.getValuesSkuJson());
        }
        return "item/index";
    }
}
